﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseCheckVLF : IssueTypeCheckBase
    {

        public DatabaseCheckVLF()
        {
            IssueTypeId = 3020;
        }

        public override void Check()
        {
            try
            {
                
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


                
                ThresholdValue thresholdValue = dc.ThresholdValues.FirstOrDefault(t => t.IssueTypeID == IssueTypeId && t.ThresholdValueID == 1);
                var Qry = from df in dc.DatabaseFiles
                          where df.nrVLF > thresholdValue.Value
                          select df;


                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                foreach (var dbfile in Qry)
                {

                    //Create new Issue

                    Issue issue = new Issue();
                    issue.IssueType = issueType;
                    issue.TargetId = dbfile.DatabaseFileID;
                    issue.Description = issueType.Description.Replace("{DatabaseFile}", dbfile.File);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

    }
}
